package com.itic.codeGen.codeGenerator.util;

import java.util.List;

import com.itic.codeGen.codeGenerator.workLine.TargetJava;
import com.itic.codeGen.codeGenerator.workLine.TargetXML;

public class ContentUtils {
	/**
	 * 
	 * @param pkage
	 * @param imports
	 * @param clzName
	 * @param superClz
	 * @param implClzs
	 * @return
	 */
	public static String getJavaContent(TargetJava target) {
		String pkage = target.getPkage();
		List<String> imports = target.getImports();
		String clzName = target.getClzName();
		String superClz = target.getSuperClz();
		List<String> implClzs = target.getImplClzs();
		List<String> header = target.getHeader();
		List<String> body = target.getBody();

		StringBuilder sb = new StringBuilder();
		// Package -- one line
		if (CommonUtils.testString(pkage)) {
			sb.append(ContentUtils.getPackageLine(pkage));
		}
		// Import -- multiple lines
		if (null != imports && imports.size() > 0) {
			sb.append(ContentUtils.getImportLine(imports));
		}
		// header
		if (null != header && header.size() > 0) {
			sb.append(ContentUtils.getHeaderLine(header));
		}
		// class+extend+implements --- one line
		// Class
		if (CommonUtils.testString(clzName)) {
			sb.append(ContentUtils.getClassLine(clzName));
		}
		// Extends
		if (CommonUtils.testString(superClz)) {
			sb.append(ContentUtils.getExtendLine(superClz));
		}
		// Implements
		if (null != implClzs && implClzs.size() > 0) {
			sb.append(ContentUtils.getImplementLine(implClzs));
		}
		// Body
		sb.append(" {");
		if (null != body && body.size() > 0) {

			sb.append(ContentUtils.getBodyLine(body,1));
		}
		sb = CommonUtils.newLine(sb);
		sb = CommonUtils.javaIndent(sb, 0);
		sb.append('}');

		// Return
		return sb.toString();
	}

	/**
	 * 
	 * @param pkage
	 * @param imports
	 * @param clzName
	 * @param superClz
	 * @param implClzs
	 * @return
	 */
	public static String getInterfaceContent(TargetJava target) {
		String pkage = target.getPkage();
		List<String> imports = target.getImports();
		String clzName = target.getClzName();
		String superClz = target.getSuperClz();
		List<String> implClzs = target.getImplClzs();
		List<String> header = target.getHeader();
		List<String> body = target.getBody();

		StringBuilder sb = new StringBuilder();
		// Package -- one line
		if (CommonUtils.testString(pkage)) {
			sb.append(ContentUtils.getPackageLine(pkage));
		}
		// Import -- multiple lines
		if (null != imports && imports.size() > 0) {
			sb.append(ContentUtils.getImportLine(imports));
		}
		// header
		if (null != header && header.size() > 0) {
			sb.append(ContentUtils.getHeaderLine(header));
		}
		// class+extend+implements --- one line
		// Class
		if (CommonUtils.testString(clzName)) {
			sb.append(ContentUtils.getInterfaceLine(clzName));
		}
		// Extends
		if (CommonUtils.testString(superClz)) {
			sb.append(ContentUtils.getExtendLine(superClz));
		}
		// Implements
		if (null != implClzs && implClzs.size() > 0) {
			sb.append(ContentUtils.getImplementLine(implClzs));
		}
		// Body
		sb.append(" {");
		if (null != body && body.size() > 0) {
			sb.append(ContentUtils.getBodyLine(body,1));
		}
		sb = CommonUtils.newLine(sb);
		sb = CommonUtils.javaIndent(sb, 0);
		sb.append('}');

		// Return
		return sb.toString();
	}

	public static String getXMLContent(TargetXML target) {
		StringBuilder sb = new StringBuilder();
		sb.append(getXMLHeader());
		sb.append("<mapper namespace=\"" + target.getBaseName() + "\">");
		sb = CommonUtils.newLine(sb);
		sb = CommonUtils.newLine(sb);
		sb.append(getBodyLine(target.getXmlSqlBody(),0));
		sb.append("</mapper>");
		return sb.toString();
	}

	// package line
	private static String getPackageLine(String packageName) {
		StringBuilder sb = new StringBuilder();
		sb.append("package "); //$NON-NLS-1$
		sb.append(packageName);
		sb.append(';');
		sb = CommonUtils.newLine(sb);
		sb = CommonUtils.newLine(sb);
		return sb.toString();
	}

	// import line
	private static String getImportLine(List<String> imports) {
		StringBuilder sb = new StringBuilder();
		for (String imp : imports) {
			sb.append("import "); //$NON-NLS-1$
			sb.append(imp);
			sb.append(';');
			sb = CommonUtils.newLine(sb);
		}
		sb = CommonUtils.newLine(sb);
		sb = CommonUtils.newLine(sb);
		return sb.toString();
	}

	// class line
	private static String getClassLine(String clzName) {
		StringBuilder sb = new StringBuilder();
		sb = CommonUtils.javaIndent(sb, 0);
		sb.append("public ");
		sb.append("class ");
		sb.append(clzName);
		return sb.toString();
	}

	// interface line
	private static String getInterfaceLine(String interfaceName) {
		StringBuilder sb = new StringBuilder();
		sb = CommonUtils.javaIndent(sb, 0);
		sb.append("public ");
		sb.append("interface ");
		sb.append(interfaceName);
		return sb.toString();
	}

	// extends line
	private static String getExtendLine(String superClz) {
		StringBuilder sb = new StringBuilder();
		sb = CommonUtils.newLine(sb);
		sb = CommonUtils.javaIndent(sb, 1);
		sb.append(" extends ");
		sb.append(superClz);
		return sb.toString();
	}

	// implements line
	private static String getImplementLine(List<String> impls) {
		StringBuilder sb = new StringBuilder();
		sb = CommonUtils.newLine(sb);
		sb = CommonUtils.javaIndent(sb, 1);
		sb.append(" implements  ");
		boolean flag = false;
		for (String imp : impls) {
			if (flag) {
				sb.append(",");
			} else {
				flag = true;
			}
			sb.append(imp);
		}
		return sb.toString();
	}

	// header line
	private static String getHeaderLine(List<String> headers) {
		StringBuilder sb = new StringBuilder();
		for (String header : headers) {
			sb.append(header);
			sb = CommonUtils.newLine(sb);
		}
		return sb.toString();
	}

	// body line
	private static String getBodyLine(List<String> bodys,int indentLevel) {
		StringBuilder sb = new StringBuilder();
		sb = CommonUtils.newLine(sb);
		for (String body : bodys) {
		    sb = CommonUtils.javaIndent(sb, indentLevel);
			sb.append(body);
			sb = CommonUtils.newLine(sb);
		}
		sb = CommonUtils.newLine(sb);
		return sb.toString();
	}

	//
	private static String getXMLHeader() {
		StringBuilder sb = new StringBuilder();
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		sb = CommonUtils.newLine(sb);
		sb.append("<!DOCTYPE mapper PUBLIC \"-//ibatis.apache.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >");
		sb = CommonUtils.newLine(sb);
		return sb.toString();
	}

}
